import Vue from "vue";
import Vuex from "vuex";
import Cookies from "js-cookie";


Vue.use(Vuex);
export default new Vuex.Store({
  state: {
    isCollspace:false,
    routerTree:"",
    sysLanguage:"",
    userInfo: {}
  },
  mutations: {
    // 存储vuex路由树
    setRouterTree(state,msg){
      state.routerTree = msg
    },
    // 设置侧边栏展开
    setCollspace(state,msg){
      state.isCollspace = msg
    },
    // 设置系统语言
    setSysLanguage(state,msg){
      Cookies.set('languages',msg)
      Vue.prototype.$_i18n.locale = msg
      state.sysLanguage = msg
      switch (msg) {
        case 'zh_cn':
          Vue.prototype.$locale.use(Vue.prototype.$zh)
          break;
        case 'en_us':
          Vue.prototype.$locale.use(Vue.prototype.$en)
          break;
        default:
          break;
      }
    },
    // 重新赋值
    removeState(state){
      if(localStorage.getItem('userInfo')){
        state.userInfo = JSON.parse(localStorage.getItem('userInfo'))
        localStorage.removeItem('userInfo')
      }
    },
    // 保存state 用户敏感信息
    saveState(state){
      localStorage.setItem('userInfo',JSON.stringify(state.userInfo))
    },
    setUserInfo(state, msg) {
      state.userInfo = {
        ...state.userInfo,
        ...msg
      }
    },
    // 清除用户信息
    clearLoginInfo(state){
      state.userInfo = {}
    },
  },
  actions: {},
  modules: {}
});
